function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}
//写cookies
function setCookie(name, value) {
	var Days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires="
			+ exp.toGMTString() + ";path=/";
}
// 读取cookies
function getCookie(name) {
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if (arr = document.cookie.match(reg))
		return unescape(arr[2]);
	else
		return null;
}
// 删除cookies
function delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 30);
	var cval = getCookie(name);
	if (cval != null)
		document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

function changeImg() {
	var imgSrc = $("#imgObj");
	var src = imgSrc.attr("src");
	// alert(src);
	imgSrc.attr("src", chgUrl(src));
}
// 时间戳
// 为了使每次生成图片不一致，即不让浏览器读缓存，所以需要加上时间戳
function chgUrl(url) {
	var timestamp = (new Date()).valueOf();
	url = url.substring(0, 17);
	if ((url.indexOf("&") >= 0)) {
		url = url + "×tamp=" + timestamp;
	} else {
		url = url + "?timestamp=" + timestamp;
	}
	return url;
}


function check() {
    var username = document.getElementById('inputUsername');
    var validcode = document.getElementById('inputValidcode');
    
    var rememberMe = document.getElementById('chkRememberMe');
    var rememberMeValue = document.getElementById('chkRememberMeValue');

    if(rememberMe.checked==true){
    	rememberMeValue.value = 1;
    }else{
    	rememberMeValue.value = 0;
    }
    
    var pcode = document.getElementById('inputPcode');
   
    console.log(rememberMe.value);
    console.log(pcode.value);
    
    //return false回阻止提交
	if (username == null || username == '') {
		alert('用户名不能为空！');
		return false;
	}
	if (pcode == null || pcode == '') {
		alert('密码不能为空！');
		return false;
	}
	if (validcode == null || validcode == '') {
		alert('密码不能为空！');
		return false;
	}

    return true;
}

function reday(){
	var msg = document.getElementById('msg');
	console.log(msg);
	console.log(msg.innerText);
	if(msg.innerText){
		msg.style.display="block";
	}else{
		msg.style.display="none";
	}
}

reday();


//////////////////验证码//////////////////////////////////////
var countdown=60; 
function settime(obj) {
	if (countdown == 0) { 
		obj.removeAttribute("disabled");    
		obj.value="获取验证码"; 
		countdown = 60; 
		return;
	} else { 
		obj.setAttribute("disabled", true); 
		obj.value="重新发送(" + countdown + ")"; 
		countdown--; 
	} 
	setTimeout(function() {settime(obj)},1000)
};



function getValidCode() {
    var pcode = document.getElementById('inputPcode');
    var username = document.getElementById('inputUsername');
	if(username.value==null || username.value==''){
		console.log('手机号/电子邮件不能为空！');
		alert('手机号/电子邮件不能为空！');
		return;
	}
	if(pcode.value==null || pcode.value==''){
		console.log('图片验证码不能为空！');
		alert('图片验证码不能为空！');
		return;
	}
	
	var options = new Object();
	options.username = username.value;
	options.pcode = pcode.value;
	options.regFlag = 0;
	
	var url = "vcode";
	var xhr=new XMLHttpRequest();
	xhr.open('POST', url, true);
	// 添加http头，发送信息至服务器时内容编码类型
	//xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.setRequestHeader('Content-Type','application/json');
	xhr.onreadystatechange=function(){
		//console.log('xhr-->>',xhr);
	    if (xhr.readyState==4 && xhr.status==200){
	    	var data = JSON.parse(xhr.responseText);
	    	console.log('data-->', data);
	    	if(data.code==0){
	    		settime(document.getElementById('vcode'));
	    		alert(data.info);
	    	}else{
		    	//alert('验证码不正确，请输入正确的验证码！');
		    	alert(data.info+'('+data.code+')');
	    	}
	    }
	}
	
	xhr.send(JSON.stringify(options));
};




